home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 2.toast / pc / sample code / printing / std file saver / source / mypack_4096.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-09-28  |  8.5 KB  |  311 lines

  1. #include <Types.h>
  2. #include <Lists.h>
  3. #include <Devices.h>
  4. #include <Controls.h>
  5. #include <TextUtils.h>
  6. #include <Resources.h>
  7. #include <Printing.h>
  8. #include <Files.h>
  9.  
  10. #include "SettingsUtils.h"
  11. #include "driverTypes.h"
  12.  
  13. typedef struct {
  14.     short            size;
  15.     ControlHandle    leftButton;
  16.     ControlHandle    rightButton;
  17.     ControlHandle    onButton;
  18.     ControlHandle    offButton;
  19. } controlsList, *controlsListPtr, **controlsListHandle;
  20.  
  21. typedef enum {
  22.     leftButtonID = 1,
  23.     rightButtonID,
  24.     onButtonID,
  25.     offButtonID,
  26.     byteMask = 0xff
  27. } buttonID;
  28.  
  29. /* control utilities */
  30. static void MySetControlVal(ControlHandle theControl,short value);
  31. static void SaveControls(controlsListPtr theControls);
  32. static controlsListHandle RetrieveControls(void);
  33. static void DisposeControls(void);
  34.  
  35. /* other util functions */
  36. static void ShowAbout(void);
  37.  
  38. /* chooser message handlers */
  39. static OSErr DoInit(controlsListPtr theControls,StringPtr zoneName);
  40. static OSErr DoAddSelection(StringPtr zoneName, ListHandle theList);
  41. static OSErr DoFillList(StringPtr zoneName, ListHandle theList);
  42. static OSErr DoGetSelection(StringPtr zoneName, StringPtr objName,
  43.     ListHandle theList);
  44. static OSErr DoSelect(StringPtr zoneName, StringPtr objName, ListHandle theList,
  45.     long rowNum);
  46. static OSErr DoDeselect(StringPtr zoneName, StringPtr objName, ListHandle theList,
  47.     long rowNum);
  48. static OSErr DoCleanup(StringPtr zoneName, ListHandle theList);
  49. static OSErr DoLeftButton(StringPtr zoneName, StringPtr objName,
  50.     ListHandle theList, short modifiers);
  51. static OSErr DoButtons(StringPtr zoneName, StringPtr objName, ListHandle theList,
  52.     buttonID whichButton, short modifiers);
  53.  
  54. pascal OSErr ChooserMain(short msg, short caller, StringPtr objName,
  55.         StringPtr zoneName, ListHandle theList, long data);
  56.  
  57. #if defined(__MWERKS__)
  58. asm void __Startup__ (void);
  59. asm void __Startup__ (void)
  60. {
  61.     BRA.S    @1
  62.     DC.W    0x5346
  63.     DC.L    0x5041434B
  64.     DC.W    0xF000
  65.     DC.W    0x0201
  66.     DC.L    0x0C13F800
  67. @1:
  68.     JMP ChooserMain
  69. }
  70. #endif /* __MWERKS */
  71.  
  72.  
  73. /*********************************************************************************/
  74. /* control utilities */
  75. static void MySetControlVal(ControlHandle theControl, short value)
  76. {
  77.     if (theControl) {
  78.         SetControlValue(theControl,value);
  79.     } else {
  80.         DebugStr("\pBogus control handle in MySetControlVal");
  81.     }
  82. }
  83.  
  84. static void SaveControls(controlsListPtr theControls)
  85. {
  86.     Str255    foo;
  87.     Handle    savedControls = NewHandleClear(sizeof(controlsList));
  88.     
  89.     foo[0] = 0;
  90.     BlockMove((void *)theControls,(void *)(*savedControls),sizeof(controlsList));
  91.     AddResource(savedControls,'CtLs',-4080,foo);
  92. }
  93.  
  94. static controlsListHandle RetrieveControls(void)
  95. {
  96.     return((controlsListHandle)GetResource('CtLs',-4080));
  97. }
  98.  
  99. static void DisposeControls(void)
  100. {
  101.     Handle    theControls = (Handle)RetrieveControls();
  102.     if (theControls) {
  103.         RemoveResource(theControls);
  104.         DisposeHandle(theControls);
  105.         UpdateResFile(CurResFile());
  106.     } else {
  107.         DebugStr("\pD'Ohh! We lost the controls!");
  108.     }
  109. }
  110.  
  111. /*********************************************************************************/
  112. /* other util functions */
  113. static void ShowAbout(void)
  114. {
  115.     Alert(-4080, nil);
  116. }
  117.  
  118. /*********************************************************************************/
  119. /* chooser message handlers */
  120. static OSErr DoInit(controlsListPtr theControls,StringPtr zoneName)
  121. {
  122.     short    state = GetSavedRadioOption();
  123.     if (!theControls) {
  124.         DebugStr("\pBad controls in DoInit");
  125.         return(nilHandleErr);
  126.     }
  127.     ShowControl(theControls->onButton);
  128.     ShowControl(theControls->offButton);
  129.     MySetControlVal(theControls->onButton,!!state);
  130.     MySetControlVal(theControls->offButton,!state);
  131.     SaveControls(theControls);
  132.     return(noErr);
  133. }
  134.  
  135. static OSErr DoAddSelection(StringPtr zoneName, ListHandle theList)
  136. {
  137.     DebugStr("\pGot Add selection message");
  138.     return(noErr);
  139. }
  140.  
  141. static OSErr DoFillList(StringPtr zoneName, ListHandle theList)
  142. {
  143.     int    i,total;
  144.     Str255    theString;
  145.     total = countChooserStrings;
  146.     for(i=0;i<total;i++) {
  147.         Cell    theCell;
  148.  
  149.         GetIndString(theString,chooserStrings,i+1);
  150.         LAddRow(1,i+1,theList);
  151.         SetPt(&theCell,0,i);
  152.         LSetCell(&(theString[1]),(short)(theString[0]),theCell,theList);
  153.     }
  154.     return(noErr);
  155. }
  156.  
  157. static OSErr DoGetSelection(StringPtr zoneName, StringPtr objName,
  158.     ListHandle theList)
  159. {
  160.     // This is called when our driver is first chosen in the chooser to
  161.     // select the zone and object name that we want selected. Since I'm
  162.     // not saving that information, I can't very well set it to anything
  163.     // so I just return noErr.
  164.     
  165.     // given that for non-appletalk devices, zoneName and objName aren't
  166.     // very useful, the API for this function doesn't seem optimal.
  167.  
  168.     //    DebugStr("\pGot get selection message");
  169.     return(noErr);
  170. }
  171.  
  172. static OSErr DoSelect(StringPtr zoneName, StringPtr objName, ListHandle theList,
  173.     long rowNum)
  174. {
  175.     // note: if we don't accept the fillListMsg, the last param to this is
  176.     // undefined. If we do accept it, the objName param is undefined, and
  177.     // the last param is the row # of the list item that was chosen.
  178.     // If we're an appletalk device, the last param is a pointer to the
  179.     // AddrBlock for the device. Isn't that simple?
  180.  
  181.     // DebugStr("\pGot select message");
  182.     return(noErr);
  183. }
  184.  
  185. static OSErr DoDeselect(StringPtr zoneName, StringPtr objName, ListHandle theList,
  186.     long rowNum)
  187. {
  188.     // note: if we don't accept the fillListMsg, the last param to this is
  189.     // undefined. If we do accept it, the objName param is undefined, and
  190.     // the last param is the row # of the list item that was chosen.
  191.     // If we're an appletalk device, the last param is a pointer to the
  192.     // AddrBlock for the device. Isn't that simple?
  193.  
  194.     // DebugStr("\pGot deselect message");
  195.     return(noErr);
  196. }
  197.  
  198. static OSErr DoCleanup(StringPtr zoneName, ListHandle theList)
  199. {
  200.     controlsListHandle    theControls = RetrieveControls();
  201.  
  202.     // notice that I only save the setting when I get the terminate
  203.     // this is to save the pounding on the ResFile if I had a LOT
  204.     // more stuff to save
  205.     if (!theControls) {
  206.         DebugStr("\pBad controls in DoCleanup");
  207.         return(nilHandleErr);
  208.     }
  209.     SaveRadioOption(GetControlValue((**theControls).onButton));
  210.     DisposeControls();
  211.     return(noErr);
  212. }
  213.  
  214. static OSErr DoLeftButton(StringPtr zoneName, StringPtr objName,
  215.     ListHandle theList, short modifiers)
  216. {
  217.     Str255    theString;
  218.     short    whichItem;
  219.     Cell    theCell;
  220.  
  221.     theCell.h = theCell.v = 0;
  222.  
  223.     // figure out which is selected -- for an appletalk device, the
  224.     // zoneName and objName would be what you'd probably want to
  225.     // look at.
  226.  
  227.     // I could also save this away when I get the select message
  228.     // and then read it back here, but the settings already
  229.     // display how to handle global data.
  230.  
  231.     if (LGetSelect(true,&theCell,theList)) whichItem = theCell.v;
  232.     else return(noErr);
  233.  
  234.     GetIndString(theString,longerStrings,whichItem+1);
  235.     ParamText(theString,nil,nil,nil);
  236.     Alert(-4081, nil);
  237.     return(noErr);
  238. }
  239.  
  240. static OSErr DoButtons(StringPtr zoneName, StringPtr objName, ListHandle theList,
  241.     buttonID whichButton, short modifiers)
  242. {
  243.     controlsListHandle    theControls = RetrieveControls();
  244.     
  245.     if (!theControls) {
  246.         DebugStr("\pEEK! Can't retrieve the control handles");
  247.         return(ResError());
  248.     }
  249.  
  250.     switch(whichButton) {
  251.         case leftButtonID:    // we've got a selection, do something with it
  252.             return(DoLeftButton(zoneName,objName,theList,modifiers));
  253.             break;
  254.         case rightButtonID:
  255.             ShowAbout();
  256.             break;
  257.         case onButtonID:
  258.             // notice that I don't save the setting every time it changes
  259.             SetControlValue((**theControls).onButton,1);
  260.             SetControlValue((**theControls).offButton,0);
  261.             break;
  262.         case offButtonID:
  263.             SetControlValue((**theControls).onButton,0);
  264.             SetControlValue((**theControls).offButton,1);
  265.             break;
  266.         default:
  267.             DebugStr("\pWhat the heck kind of button is that?");
  268.             break;
  269.     }
  270.     return(noErr);
  271. }
  272.  
  273. pascal OSErr ChooserMain(short msg, short caller, StringPtr objName,
  274.         StringPtr zoneName, ListHandle theList, long data)
  275. {
  276.     OSErr    error = noErr;
  277.     if (caller != chooserID) {
  278.         DebugStr("\pHmm. We got called by someone that isn't the chooser");
  279.     }
  280.     switch(msg) {
  281.         case chooserInitMsg:
  282.             error = DoInit((controlsListPtr)objName,zoneName);
  283.             break;
  284.         case newSelMsg:
  285.             error = DoAddSelection(zoneName,theList);
  286.             break;
  287.         case fillListMsg:
  288.             error = DoFillList(zoneName, theList);
  289.             break;
  290.         case getSelMsg:
  291.             error = DoGetSelection(zoneName, objName, theList);
  292.             break;
  293.         case selectMsg:
  294.             error = DoSelect(zoneName, objName, theList, data);
  295.             break;
  296.         case deselectMsg:
  297.             error = DoDeselect(zoneName, objName, theList, data);
  298.             break;
  299.         case terminateMsg:
  300.             error = DoCleanup(zoneName, theList);
  301.             break;
  302.         case buttonMsg:
  303.             error = DoButtons(zoneName, objName, theList, (data & byteMask),
  304.                                 (short)(data >> 16));
  305.             break;
  306.         default:
  307.             DebugStr("\pWhat the heck message was that?");
  308.             break;
  309.     }
  310.     return(error);
  311. }